﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;
using pklib;

namespace pklib {
	/// <summary>
	/// Interaction logic for TimePicker.xaml
	/// </summary>
	public partial class TimePicker : UserControl {

		public static DependencyProperty TimeProperty;

		public static void TimePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			(d as TimePicker).Selected = (DateTime)e.NewValue;
		}

		static TimePicker() {
			FrameworkPropertyMetadata PropertyMetaData = new FrameworkPropertyMetadata(TimePropertyChanged);

			PropertyMetaData.BindsTwoWayByDefault = true;
			TimePicker.TimeProperty = DependencyProperty.Register(
				"Time", typeof(DateTime), typeof(TimePicker), PropertyMetaData);
		}
		
		public DateTime Time {	
			get { return (DateTime)GetValue(TimeProperty);	}
			set { SetValue(TimeProperty, value); }
		}

		private DateTime _Selected = DateTime.MinValue;
		public DateTime Selected {
			get { return this._Selected; }
			set {
				TextBlock tb;
				foreach ( Border b in this.FindElements(this._Selected) ) {
					b.Background = Brushes.White;
					tb = XOM.Down(b, typeof(TextBlock)) as TextBlock;
					if(tb != null)
						tb.Foreground = SystemColors.ControlTextBrush;
				}
				this._Selected = value;
				this.Time = value;
				foreach ( Border b in this.FindElements(this._Selected) ) {
					b.Background = SystemColors.HighlightBrush;
					tb = XOM.Down(b, typeof(TextBlock)) as TextBlock;
					if(tb != null)
						tb.Foreground = SystemColors.HighlightTextBrush;
				}
			}
		}

		public TimePicker() {
			InitializeComponent();
		}

		protected override void OnMouseDown(MouseButtonEventArgs e) {
			base.OnMouseDown(e);

			Border b = null;
			if( e.OriginalSource.GetType() == typeof(TextBlock) )
				b = XOM.Up(e.OriginalSource as DependencyObject, typeof(Border), 2) as Border;
			if( e.OriginalSource.GetType() == typeof(Border)) {
				if ( (e.OriginalSource as Border).Name == "" )
					b = XOM.Up(e.OriginalSource as DependencyObject, typeof(Border)) as Border;
				else
					b = e.OriginalSource as Border;
			}
			if ( b != null && b.Name.StartsWith("Hour") ) {
				this.Selected = new DateTime(this._Selected.Year, this._Selected.Month, this._Selected.Day,
					Int32.Parse(b.Name.Substring(4)), this._Selected.Minute, 0);
			}
			if ( b != null && b.Name.StartsWith("Minute") ) {
				this.Selected = new DateTime(this._Selected.Year, this._Selected.Month, this._Selected.Day,
					this._Selected.Hour, Int32.Parse(b.Name.Substring(6)), 0);
			}
		}

		private IEnumerable FindElements(DateTime Time) {
			List<FrameworkElement>		elems = new List<FrameworkElement>();
			FrameworkElement			b;

			if( (b = this.FindName(String.Format("Hour{0}", Time.ToString("HH"))) as FrameworkElement) != null )
				elems.Add(b);

			if( (b = this.FindName(String.Format("Minute{0,2:00}", Math.Round((Decimal)Time.Minute / 5)*5)) as FrameworkElement) != null )
				elems.Add(b);
			return elems;
		}
	}
}
